import HttpUtils from "../../common/utils/HttpUtils";
import Carousel from "@ui/RJMart/carousel";
import Rest from "@module/Rest";

export default class CarouselComp extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            banners: []
        };
    }

    componentDidMount() {
        // HttpUtils.getBNNER()
        //     .then(res => {
        //         console.log(res);
        //         this.setState({
        //             banners: res.ads.MAIN_BNNER
        //         });
        //     })
        //     .catch(err => {
        //         console.log(err);
        //     });
        // let url = `/shop/noLogin/poster/queryAdvertListByAdvertGroup`;
        let url = `/store/api/homepage/advert/queryAdvertListByAdvertGroup
        `;
        Rest.post(url, { groupCode: "G-1-1-1-1" }).then(res => {
            console.log(res);
            let bannerList = [];
            if (res.code == "200") {
                res.data &&
                    res.data.forEach((item, index) => {
                        item.imgurl &&
                            bannerList.push({
                                targetUrl: item.advertUrl,
                                imgSrc: item.imgurl
                            });
                    });
                if (bannerList.length > 0) {
                    this.setState({
                        banners: bannerList
                    });
                } else {
                    this.getGroupList();
                }
            }
        });
    }

    getGroupList = () => {
        let url = `/shop/noLogin/poster/queryAdvertGroupList`;
        Rest.post(url, {groupCode:'G-1-1-1-1',terminalPosition:1,showType:1})
            .then(res => {
                console.log(res);
                let bannerList = [];
                if(res.code=='1') {
                    res.data&&res.data.forEach((item,index)=>{
                        if(item.groupCode=='G-1-1-1-1') {
                            item.bizAdvertPositionVOList.forEach((it,i)=>{
                                let defaultAdvert=JSON.parse(it.defaultAdvert)||{};
                                bannerList.push({
                                    targetUrl: defaultAdvert.url,
                                    imgSrc: defaultAdvert.imgUrl
                                })
                            })
                        }
                    })
                }
                this.setState({
                    banners: bannerList
                });
            },err=>{

            })
           
    };

    render() {
        // const {Carousel,Card,}=RJMart
        const { banners } = this.state;

        return (
            <div className={"carousel-comp-box"}>
                <Carousel
                    autoPlay
                    background="#fff"
                    dotShape="dot"
                    height="440"
                    duration={5000}
                >
                    {banners &&
                        banners.map((item, index) => (
                            <div
                                key={index}
                                onClick={() => {
                                    // window.location.href=item.targetUrl
                                    window.open(item.targetUrl);
                                }}
                            >
                                <img
                                    src={item.imgSrc}
                                    alt=""
                                    className={"carousel-comp-img"}
                                />
                            </div>
                        ))}
                </Carousel>
            </div>
        );
    }
}
